package com.shawn.server.Advice;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSException;
import com.aliyun.tea.TeaException;
import com.shawn.model.vo.ErrorResult;
import com.shawn.server.exception.BusinessException;
import com.shawn.server.exception.StrangerQuestionException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.text.ParseException;

/**
 *
 *   全局异常处理类
 *   使用@ControllerAdvice来声明一个类可以用来统一对所有@RequestMapping方法来做@ExceptionHandler、@InitBinder以及@ModelAttribute处理。
 * @author lenovo
 * @date 2022/10/26
 */
@ControllerAdvice(annotations = {RestController.class, Controller.class})
@ResponseBody
@Slf4j
public class ExceptionAdvice {
    /**
     * 处理业务异常
     * @param be 是
     * @return {@link ResponseEntity}
     */
    @ExceptionHandler(BusinessException.class)
    public ResponseEntity businessException(BusinessException be){
        be.printStackTrace();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(be.getErrorResult());
    }

    /**
     * 统一默认问题
     * @param be 是
     * @return {@link ResponseEntity}
     */
    @ExceptionHandler(StrangerQuestionException.class)
    public ResponseEntity questionException(StrangerQuestionException be){
        be.printStackTrace();
        return ResponseEntity.ok(be.getMessage());
    }

    /**
     * 解析异常
     *
     * @param pe 体育
     * @return {@link ResponseEntity}
     */
    @ExceptionHandler(ParseException.class)
    public ResponseEntity parseException(ParseException pe){
        pe.printStackTrace();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ErrorResult.parseError());
    }

    /**
     * IO exception
     *
     * @param ioe 流异常
     * @return {@link ResponseEntity}
     */
    @ExceptionHandler(IOException.class)
    public ResponseEntity iOException(IOException ioe){
        ioe.printStackTrace();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ErrorResult.parseError());
    }

    /**
     * 文件上传异常
     *
     * @param osse osse
     * @return {@link ResponseEntity}
     */
    @ExceptionHandler(OSSException.class)
    public ResponseEntity oSSException(OSSException osse){
        System.out.println("Caught an OSSException, which means your request made it to OSS, "
                + "but was rejected with an error response for some reason.");
        System.out.println("Error Message:" + osse.getErrorMessage());
        System.out.println("Error Code:" + osse.getErrorCode());
        System.out.println("Request ID:" + osse.getRequestId());
        System.out.println("Host ID:" + osse.getHostId());
        osse.printStackTrace();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ErrorResult.osseError());

    }

    /**
     * 客户端异常
     *
     * @param ce ce
     * @return {@link ResponseEntity}
     */
    @ExceptionHandler(ClientException.class)
    public ResponseEntity clientException(ClientException ce){
        System.out.println("Caught an ClientException, which means the client encountered "
                + "a serious internal problem while trying to communicate with OSS, "
                + "such as not being able to access the network.");
        System.out.println("Error Message:" + ce.getMessage());
        ce.printStackTrace();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ErrorResult.clientError());
    }

    /**
     * 验证码发送异常
     *
     * @param te te
     * @return {@link ResponseEntity}
     */
    @ExceptionHandler(TeaException.class)
    public ResponseEntity teaException(TeaException te){
        te.printStackTrace();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ErrorResult.teaError());
    }



    /**
     * 处理未知异常
     * @param e e
     * @return {@link ResponseEntity}
     */
    @ExceptionHandler(Exception.class)
    public ResponseEntity exception(Exception e){
        e.printStackTrace();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ErrorResult.error());
    }



}
